package com.diemat.tools;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.photos.*;
import com.google.gdata.data.photos.*;
import com.google.gdata.util.ServiceException;

public class PicasaAccess {

	static final PicasawebService myService = new PicasawebService("Diemat",
			"Gallery_CSM_Puteaux_HB", "1.0");

	static final String API_PREFIX = "https://picasaweb.google.com/data/feed/api/user/";

	public List<AlbumEntry> getAlbumListForUser(String username) {

		List<AlbumEntry> myAlbumList = null;
		URL feedUrl;
		try {
			feedUrl = new URL(API_PREFIX + username + "?kind=album");
			UserFeed myUserFeed = myService.getFeed(feedUrl, UserFeed.class);

			myAlbumList = myUserFeed.getAlbumEntries();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return myAlbumList;
	}

	public String getAlbumCover(AlbumEntry album) {
		String coverUrl = album.getMediaThumbnails().get(0).getUrl();
		return coverUrl;
	}

	public AlbumFeed getAlbumFeed(String username, String albumId) {
		AlbumFeed albumFeed = null;
		URL feedUrl;
		try {
			feedUrl = new URL(API_PREFIX + username + "/albumid/" + albumId);
			albumFeed = myService.getFeed(feedUrl, AlbumFeed.class);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return albumFeed;
	}
}
